﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class DepartmentDal
    {
        public DapperHelper dapperHelper { get; set; }

        /// <summary>
        /// 获取所有科室
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DepartmentView> GetAll() {
            return dapperHelper.ExecueWithoutTransaction<IEnumerable<DepartmentView>>(a =>
            {
               
                string sql = @"select * from AIER_DEPARTMENT_INFO /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                return a.Query<DepartmentView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }

        /// <summary>
        /// 根据名字查询科室id(QueryDepartmentIdByName)
        /// </summary>
        /// <param name="hospitalId"></param>
        /// <returns></returns>
        public IEnumerable<DepartmentView> QueryDepartmentIdByName(Guid siteId, string name) {
           
           return dapperHelper.ExecueWithoutTransaction<IEnumerable<DepartmentView>>(a =>
            {
                if (siteId==Guid.Empty) { return null; }
                string sql = @"select * from AIER_DEPARTMENT_INFO /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                
                sqlBuilder.Where("siteid = @siteId", new { siteId = siteId });
                if (!string.IsNullOrWhiteSpace(name))
                {
                    sqlBuilder.Where("departmentName = @departmentName", new { departmentName = name });
                }
                return a.Query<DepartmentView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }

        /// <summary>
        /// 根据医院Id获取科室信息
        /// </summary>
        /// <param name="hospitalId"></param>
        /// <returns></returns>
        public IEnumerable<DepartmentView> GetDepartmentByHospitalId(Guid hospitalId)
        {
            return new DapperHelper().ExecueWithoutTransaction(select =>
            {
                string strsql = @"select new_hospital SiteId,new_departmentId,new_name departmentName from new_department 
                                      /**where**/ ";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(strsql);
                sqlBuilder.Where("new_hospital = @hospitalId", new { hospitalId = hospitalId });
                return select.Query<DepartmentView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }
    }
}
